﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhaoxi.SqlSugar.Common;
using Zhaoxi.SqlSugar.Models;

namespace Zhaoxi.SqlSugarAdvanced.SaasSubDatabase
{
    /// <summary>
    /// 分库操作
    /// </summary>
    public class SubDatabaseInfo
    {
        /// <summary>
        /// 分库操作实例
        /// </summary>
        public static void Show()
        {
            List<ConnectionConfig> configList = new List<ConnectionConfig>()
            {
                new ConnectionConfig()
                {
                    ConfigId= 1,
                    ConnectionString = CustomConnectionConfig.ConnectionString001,
                    IsAutoCloseConnection = true,
                    DbType = DbType.SqlServer
                },
                new ConnectionConfig()
                {
                    ConfigId= 2,
                    ConnectionString = CustomConnectionConfig.ConnectionString003,
                    IsAutoCloseConnection = true,
                    DbType = DbType.SqlServer
                }
            };

            ConnectionConfig connectionConfig3 = new ConnectionConfig()
            {
                ConfigId = 3,
                ConnectionString = CustomConnectionConfig.ConnectionString004,
                IsAutoCloseConnection = true,
                DbType = DbType.SqlServer
            };


            OrderHead orderHead = new OrderHead()
            {
                CreateTime = DateTime.Now,
                Id = 1,
                Name = "测试数据",
                Price = 345
            }; 
            OrderItemNew orderItemNew1 = new OrderItemNew()
            {
                CreateTime = DateTime.Now,
                Price = 399,
                OrderId = 1,
                Description = "测试数据"
            };
            OrderItemNew orderItemNew2 = new OrderItemNew()
            {
                CreateTime = DateTime.Now,
                Price = 399,
                OrderId = 1,
                Description = "测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数测试数"
            };


            using (SqlSugarScope sqlSugarScope = new SqlSugarScope(configList))
            {
                sqlSugarScope.AddConnection(connectionConfig3); // 添加数据库3的连接

                if (sqlSugarScope.GetConnection(2) // 指定数据库连接的ConfigId
                    .DbMaintenance.IsAnyTable("OrderItemNew", false))
                {
                    sqlSugarScope.GetConnection(2)// 指定数据库连接的ConfigId
                        .DbMaintenance.DropTable<OrderItemNew>();
                }
                sqlSugarScope.GetConnection(2)// 指定数据库连接的ConfigId
                    .CodeFirst.InitTables(typeof(OrderItemNew));


                sqlSugarScope.GetConnection(3)// 指定数据库连接的ConfigId
                    .DbMaintenance.CreateDatabase(); // 创建数据库
                if (sqlSugarScope.GetConnection(3)// 指定数据库连接的ConfigId
                    .DbMaintenance.IsAnyTable("OrderItemNew", false))
                {
                    sqlSugarScope.GetConnection(3)// 指定数据库连接的ConfigId
                        .DbMaintenance.DropTable<OrderItemNew>(); // 删除表
                }
                sqlSugarScope.GetConnection(3)// 指定数据库连接的ConfigId
                    .CodeFirst.InitTables(typeof(OrderItemNew)); // 创建表


                try
                {
                    sqlSugarScope.GetConnection(1)// 指定数据库连接的ConfigId
                        .Deleteable<OrderHead>().ExecuteCommand();
                    sqlSugarScope.GetConnection(2)// 指定数据库连接的ConfigId
                        .Deleteable<OrderItemNew>().ExecuteCommand();
                    sqlSugarScope.GetConnection(3)// 指定数据库连接的ConfigId
                        .Deleteable<OrderItemNew>().ExecuteCommand();


                    sqlSugarScope.BeginTran();
                    sqlSugarScope.GetConnection(1)// 指定数据库连接的ConfigId
                        .Insertable<OrderHead>(orderHead).ExecuteCommand();
                    sqlSugarScope.GetConnection(2)// 指定数据库连接的ConfigId
                        .Insertable<OrderItemNew>(orderItemNew1).ExecuteCommand();
                    sqlSugarScope.GetConnection(3)// 指定数据库连接的ConfigId
                        .Insertable<OrderItemNew>(orderItemNew2).ExecuteCommand();

                    sqlSugarScope.CommitTran();
                }
                catch (Exception)
                {
                    sqlSugarScope.RollbackTran();
                }
            }
        }


    }
}
